package cn.e4j.modular.system.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.entity.SysUserRole;
import cn.e4j.modular.system.mapper.SysRoleMapper;
import cn.e4j.modular.system.mapper.SysUserMapper;
import cn.e4j.modular.system.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户资料表 服务实现类
 * </p>
 *
 * @author wenchuan.li
 * @since 2018-12-02
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getUserslist(Page<Object> page, Dto dto) {

        //List<Map<String, Object>> userDtos = InvokeUtil.invokeBeanMethod("sysUserMapper,listUsersPage", page, dto);


        List<Map<String, Object>> userDtos = this.baseMapper.listUsersPage(page, dto);
        for (Map<String, Object> map : userDtos) {
            String roles_ = "";
            List<Map<String, Object>> listRole = sysRoleMapper.listRolesOfUser(map.get("ID").toString());
            for (Map<String, Object> item : listRole) {
                roles_ = roles_ + item.get("NAME").toString() + " | ";
            }
            map.put("sign", StringUtils.substringBeforeLast(roles_, "|"));
        }
        return userDtos;
    }

    @Transactional(readOnly = true)
    public List<SysUser> getUserslist(Dto dto) {

        List<SysUser> userDtos = this.baseMapper.listUsersExports(dto);
        return userDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listUserRoles(String userid) {
        return this.baseMapper.listUserRoles(userid);
    }

    @Override
    public SysUser getByAccount(String account) {
        return this.baseMapper.getByAccount(account);
    }

    @Override
    @Transactional
    public void delUser(SysUser user) {
        user.deleteById();

        //删除用户对应的角色资料
        SysUserRole sysUserRole = new SysUserRole();
        UpdateWrapper<SysUserRole> userRoleUpdateWrapper = new UpdateWrapper<>();
        userRoleUpdateWrapper.eq("USER_ID", user.getID());
        sysUserRole.delete(userRoleUpdateWrapper);
    }

//    @Transactional(readOnly = true)
//    public List<Map<String, Object>> listGwaes(Dto inDto) {
//        return this.baseMapper.listGwaesExport(inDto);
//    }
//
//    @Transactional(readOnly = true)
//    public List<Map<String, Object>> listUserGwaes(String userid) {
//        return this.baseMapper.listUserGwaes(userid);
//    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listGwbws(Dto inDto) {
        return this.baseMapper.listGwbwsExport(inDto);
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listUserGwbws(String userid) {
        return this.baseMapper.listUserGwbws(userid);
    }

    @Transactional(readOnly = true)
    public Dto getUser(String ID) {
        return this.baseMapper.getUser(ID);
    }

    @Transactional(readOnly = true)
    public Dto checkCellPhone(Dto inDto) {
        return this.baseMapper.checkCellPhone(inDto);
    }

    @Transactional(readOnly = true)
    public void saveNewPassword(Dto inDto) {
        this.baseMapper.saveNewPassword(inDto);
    }

    @Transactional(readOnly = true)
    public Dto getUserRole(Dto inDto) {
        return this.baseMapper.getUserRole(inDto);
    }

    @Transactional(readOnly = true)
    public void deleteUserRole(Dto inDto) {
        this.baseMapper.deleteUserRole(inDto);
    }

    @Transactional(readOnly = true)
    public int getUserCount() {
        return this.baseMapper.getUserCount();
    }
}
